package com.example.teaculture.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.example.teaculture.R;
import com.example.teaculture.model.TeaBusiness;
import com.example.teaculture.model.TeaInfo;
import com.example.teaculture.tools.JsoupUtils;
import com.example.teaculture.tools.OkHttpUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestActivity extends AppCompatActivity {

    private static final String BASE_URL = "http://zhannei.baidu.com/cse/search?q=%E8%8C%B6%E7%BB%8F%E8%90%A5&p="+ 0 +"&s=2942127813356630428&nsid=&entry=1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Test();
    }

    private void Test() {
//        String s1 = "安吉白";
//        String s2 = "安吉白231232啥都iajs";
//        int i = s2.indexOf(s1);
//        Log.e("TAG", "Test: " + i);
//        List<TeaInfo> teaInfo = JsoupUtils.getTeaInfo("https://www.ybtea.cn/", "cyzl/lvcha/");
//        System.out.println(teaInfo);


        List<TeaBusiness> list = new ArrayList<>();
        new Thread(() -> {
            try {
                Document doc = Jsoup.connect(BASE_URL).get();
                Elements elements = doc.select("div.result");
                System.out.println(elements);
                for (Element element : elements) {
                    String title = element.select("h3.c-title").select("a").text();
                    Log.e("TAG", "Test: " + title);
                    String url = element.select("h3.c-title").select("a").attr("href");
                    Log.e("TAG", "Test: " + url);
                    String info = element.select("div.c-abstract").text();
                    Log.e("TAG", "Test: " + info);
                    String image = element.select("td").select("img").attr("src");
                    Log.e("TAG", "Test: " + image);
                    String date = element.select("div").select("span.c-showurl").text().split(" ")[1];
                    Log.e("TAG", "Test: " + date);
                    TeaBusiness teaBusiness = new TeaBusiness("1", url, date, image);
                    list.add(teaBusiness);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


//        http://www.zgchawang.com/news/show-69703.html

//        new Thread(() -> {
//            try {
//                Document doc = Jsoup.connect("http://www.zgchawang.com/news/show-69703.html").get();
//                Elements elements = doc.select("div.content");
//                System.out.println(elements);
//                for (Element element : elements) {
//                    String image = element.select("center").select("img").attr("src");
//                    System.out.println(image);
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }).start();

    }
}